#include "../../include/server/Logger.hpp"
#include "../../include/log4cpp/Priority.hh"
#include "../../include/log4cpp/PatternLayout.hh"
#include "../../include/log4cpp/FileAppender.hh"
#include "../../include/log4cpp/OstreamAppender.hh"
#include <iostream>

using std::cout;
using std::endl;

Logger *Logger::_pInstance = Logger::getInstance();

Logger *Logger::getInstance()
{
    if(nullptr == _pInstance)
    {
        _pInstance = new Logger();
        atexit(destroy);
    }

    return _pInstance;
}

void Logger::destroy()
{
    if(_pInstance)
    {
        delete _pInstance;
        _pInstance = nullptr;
    }   
}


void Logger::warn(const char *msg)
{
    /* cout << "warn()" << endl; */
    _mycat.warn(msg);
}

void Logger::error(const char *msg)
{
    /* cout << "error()" << endl; */
    _mycat.error(msg);
}

void Logger::debug(const char *msg)
{
    /* cout << "debug()" << endl; */
    _mycat.debug(msg);
}

void Logger::info(const char *msg)
{
    /* cout << "info()" << endl; */
    _mycat.info(msg);
}

 Logger::Logger()
:_mycat(Category::getRoot().getInstance("mycat"))
{
    //1.日志的格式
    PatternLayout *ppl = new PatternLayout();
    ppl->setConversionPattern("%d %c [%p] %m%n");

    //2.日志的目的地,在文件后面追加写日志
    FileAppender *pfa = new FileAppender("FileAppender", "../../log/log.dat", true);
    pfa->setLayout(ppl);
    
    //3.绑定目的地
    _mycat.addAppender(pfa);

    //4.过滤器
    _mycat.setPriority(Priority::DEBUG);
}

 Logger::~Logger()
{
    //关闭日志
    Category::shutdown();
}
